/**************************************************************************
Step 1. Include necessary header files such that the stuff your test logic needs is declared. 
Don't forget gtest.h, which declares the testing framework.                                                   
**************************************************************************/
#include "gtest/gtest.h"
#include "include/pf_sha1.h"


/**************************************************************************
Step 2. Use test fixture to design your complex cases. 
You can add SetUp() to initialize the test data and 
TearDown() to clear the test data for common usage
Pls remember that when you using test fixture, need to use macro "TEST_F" instead of "TEST"                                                 
**************************************************************************/
class PfSha1Test : public testing::Test 
{
    /* This SetUp() will be called before every TEST CASE which is marked by macro "TEST_F" */
    virtual void SetUp() 
    {

    }

    /* This TearDown will be called after every TEST CASE which is marked by macro "TEST_F" */
    virtual void TearDown() 
    {

    }

};

/***************************************************************************
Step 3. Use macro "TEST_F" to start your test case                                      
You can design your test case as the example
***************************************************************************/
TEST_F(PfSha1Test, NormalTest)
{
    /* Define the local variables */
    int ret = 0;
    pf_sha1_t* sha1_t = NULL;
    char* src_str ="qyouwerywoew.xyouwer.xppweir2937472302724,cxtiyw34234230s7w34234.you734.234youwer"; 
    unsigned char expected_str[] = {0x47, 0x0c, 0x98, 0x59, 0xe7, 0x65, 0x91, 0x43, 0x21, 0x43, 
                                    0xf4, 0x18, 0xd4, 0x99, 0x85, 0xba, 0x95, 0x5c, 0xe5, 0x55};
    unsigned char sha1_digest[20] = {0};
    
    /* initilize test data for this case */
    sha1_t = pf_sha1_start();
    EXPECT_NE((void *)NULL, sha1_t);

    /* Call your function for test */
    /* Check the result */
    ret = pf_sha1_append(sha1_t, src_str, strlen(src_str));
    EXPECT_EQ(0, ret);

    ret = pf_sha1_finish(sha1_t, sha1_digest);
    EXPECT_EQ(0, ret);

    EXPECT_EQ(0, memcmp(expected_str, sha1_digest, sizeof(sha1_digest)));

    /* Clear the test data for this case */
    sha1_t = NULL;

}

TEST_F(PfSha1Test, Sha1AppendTest)
{
    /* Define the local variables */
    int ret = -1;
    pf_sha1_t* sha1_t = NULL;
    char* src_str[] = {"uc.", "cn", " ", "uc.com"}; 
    unsigned char expected_str[] = {0x4f, 0x75, 0x4c, 0x77, 0x04, 0x4c, 0xe4, 0x2b, 0x53, 0xfe, 
                                    0x80, 0x84, 0xa7, 0x54, 0x5d, 0xaa, 0x51, 0xe1, 0x76, 0x52};
    unsigned char sha1_digest[20] = {0};
    
    /* initilize test data for this case */
    /* Call your function for test */
    /* Check the result */
    sha1_t = pf_sha1_start();
    EXPECT_NE((void *)NULL, sha1_t);

    ret = pf_sha1_append(sha1_t, src_str[0], strlen(src_str[0]));
    EXPECT_EQ(0, ret);
    ret = pf_sha1_append(sha1_t, src_str[1], strlen(src_str[1]));
    EXPECT_EQ(0, ret);
    ret = pf_sha1_append(sha1_t, src_str[2], strlen(src_str[2]));
    EXPECT_EQ(0, ret);
    ret = pf_sha1_append(sha1_t, src_str[3], strlen(src_str[3]));
    EXPECT_EQ(0, ret);

    ret = pf_sha1_finish(sha1_t, sha1_digest);
    EXPECT_EQ(0, ret);

    EXPECT_EQ(0, memcmp(expected_str, sha1_digest, sizeof(sha1_digest)));

    /* Clear the test data for this case */
    sha1_t = NULL;

}

TEST_F(PfSha1Test, Sha1ZeroTest)
{
    ::testing::FLAGS_gtest_death_test_style = "threadsafe"; /* avoid death warning */

    /* Define the local variables */
    int ret = -1;
    pf_sha1_t* sha1_t = NULL;
    char* src_str = ""; 
    unsigned char expected_str[] = {0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 
                                    0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09};
    unsigned char sha1_digest[20] = {0};

    /* initilize test data for this case */
    /* Call your function for test */
    /* Check the result */
    sha1_t = pf_sha1_start();
    EXPECT_NE((void *)NULL, sha1_t);

    ret = pf_sha1_append(sha1_t, src_str, strlen(src_str));
    EXPECT_EQ(0, ret);

    ret = pf_sha1_finish(sha1_t, sha1_digest);
    EXPECT_EQ(0, ret);

    EXPECT_EQ(0, memcmp(expected_str, sha1_digest, sizeof(sha1_digest)));

    /* Clear the test data for this case */
    sha1_t = NULL;
}

#ifndef VALGRIND_CHECK /* Death test core dump makes valgrind too many errors */
TEST_F(PfSha1Test, Sha1DeathTest)
{
    ::testing::FLAGS_gtest_death_test_style = "threadsafe"; /* avoid death warning */

    /* Define the local variables */
    int ret = -1;
    pf_sha1_t* sha1_t = NULL;
    char* src_str = "uc.cn uc.com"; 
    //unsigned char expected_str[] = {0x4f, 0x75, 0x4c, 0x77, 0x04, 0x4c, 0xe4, 0x2b, 0x53, 0xfe, 
    //                                0x80, 0x84, 0xa7, 0x54, 0x5d, 0xaa, 0x51, 0xe1, 0x76, 0x52};
    unsigned char sha1_digest[20] = {0};

    /* initilize test data for this case */
    /* Call your function for test */
    /* Check the result */
    sha1_t = pf_sha1_start();
    EXPECT_NE((void *)NULL, sha1_t);

    //ret = pf_sha1_append(sha1_t, src_str, strlen(src_str));
    //EXPECT_EQ(0, ret);
    //ret = pf_sha1_append(sha1_t, src_str, -1);
    EXPECT_DEBUG_DEATH((ret = pf_sha1_append(NULL, src_str, strlen(src_str))), "");
    EXPECT_DEBUG_DEATH((ret = pf_sha1_append(sha1_t, NULL, strlen(src_str))), "");
    EXPECT_DEBUG_DEATH((ret = pf_sha1_append(sha1_t, src_str, -1)), "");

    ret = pf_sha1_finish(sha1_t, sha1_digest);
    EXPECT_EQ(0, ret);

    /* Clear the test data for this case */
    sha1_t = NULL;
}
#endif /* #ifndef VALGRIND_CHECK */
